package com.espirit.common;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Transient;

import com.espirit.EntityMapping;
import com.espirit.eap.manager.Department;
import com.espirit.eap.manager.EntityStatus;
import com.espirit.eap.manager.ProofNbrAware;
import com.espirit.eap.manager.workflow.WorkflowAware;
import com.espirit.eap.sa.Employee;
import com.espirit.eap.sa.EmployeeAware;
import com.espirit.eap.sa.User;
import com.googlecode.cswish.exception.ApplicationException;
import com.googlecode.cswish.model.Saas;

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Form extends EntityMapping implements Serializable, EmployeeAware, WorkflowAware, VersionAware, Saas, ProofNbrAware{
	private static final long serialVersionUID = 5391093825175243403L;
	private List<RelatedFile> relatedFiles;
	private String remark;
	private Date createdOn;
	private User created;
	private Employee employee;
	private Department department;
	private String deptIds;
	private EntityStatus entityStatus;
	
	private Integer sid;
	public Integer getSid() {
		return sid;
	}

	public void setSid(Integer sid) {
		this.sid = sid;
	}
	
	@ManyToOne(fetch=FetchType.LAZY)
	public Employee getEmployee() {
		return employee;
	}
	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	@ManyToOne(fetch=FetchType.LAZY)
	public Department getDepartment() {
		return department;
	}
	public void setDepartment(Department department) {
		this.department = department;
	}

	@Transient
	public String getDeptIds() {
		return deptIds;
	}
	public void setDeptIds(String deptIds) {
		this.deptIds = deptIds;
	}
	
	@Transient
	public String getSrcEntityKey() {
		return "proofNbr";
	}

	public void onSetEntityStatus(EntityStatus orderStatus) {
		if (orderStatus != null){
			if (orderStatus.isAccept()){
				onAccept();
			} else if (orderStatus.isReject()){
				onReject();
			}
		}
		setEntityStatus(orderStatus);
	}
	
	protected void onReject() {
		
	}
	
	@Override
	public void onWFStarted() {
		if (!canRefer()){
			throw new ApplicationException("errors.event.refered");
		}
		
	}

	@Transient
	public boolean isAccept(){
		return (this.entityStatus != null && this.entityStatus.isAccept());
	}
	
	@Override
	public void onWFAccept() {
		
	}

	@Override
	public void onWFReject() {
		
	}


	@OneToMany(targetEntity=RelatedFile.class,orphanRemoval = true,fetch=FetchType.LAZY,mappedBy="form", cascade=CascadeType.ALL)
	public List<RelatedFile> getRelatedFiles() {
		return relatedFiles;
	}
	public void setRelatedFiles(List<RelatedFile> relatedFiles) {
		this.relatedFiles = relatedFiles;
	}
	
	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}
	
	public Date getCreatedOn() {
		return createdOn;
	}
	public void setCreatedOn(Date createdOn) {
		this.createdOn = createdOn;
	}
	@ManyToOne(targetEntity = User.class,fetch=FetchType.LAZY)
	public User getCreated() {
		return created;
	}
	public void setCreated(User created) {
		this.created = created;
	}
	
	private WFMessage wfMessage;
	@Transient
	public WFMessage getWfMessage() {
		return wfMessage;
	}
	public void setWfMessage(WFMessage wfMessage) {
		this.wfMessage = wfMessage;
	}
	
	@ManyToOne(targetEntity=EntityStatus.class,fetch=FetchType.LAZY)
	public EntityStatus getEntityStatus() {
		return entityStatus;
	}
	public void setEntityStatus(EntityStatus entityStatus) {
		this.entityStatus = entityStatus;
	}
	
	public boolean canRefer(){
		if (entityStatus == null) return true;
		if (entityStatus.isAccept() || entityStatus.isRefer() || entityStatus.isDeleted()){ 
			return false;
		} 
		return true;
	}
	
	public boolean canAccept(){
		if (entityStatus == null) return false;
		return (entityStatus.isRefer());
	}
	
	public boolean canDelete(){
		if (entityStatus == null) return true;
		return entityStatus.isInited();
	}
	
	public boolean canUpdate(){
		if (entityStatus == null) return true;
		return !entityStatus.isAccept();
	}
	
	protected void onAccept(){
		if (entityStatus != null && entityStatus.isAccept()){
			throw new ApplicationException("errors.workflow.accept.cannt", getId());
		}
	}

	private String proofNbr;
	@Override
	public String getProofNbr() {
		return proofNbr;
	}
	public void setProofNbr(String proofNbr){
		this.proofNbr = proofNbr;
	}

}
